package com.ggh.jinjilive.ui;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.common.Constants;
import com.ggh.jinjilive.databinding.ActivityTotalPageBinding;
import com.ggh.jinjilive.dialog.ReleaseDialog;
import com.ggh.jinjilive.ui.home.HomeFragment;
import com.ggh.jinjilive.ui.mall.MallFragment;
import com.ggh.jinjilive.ui.mine.MineFragment;
import com.ggh.jinjilive.ui.social.SocialFragment;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.ggh.jinjilive.utils.GeneralUtils;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.XXPermissions;
import com.huantansheng.easyphotos.EasyPhotos;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;


@Route(path = ARouterUtils.TotalPageActivity)
public class TotalPageActivity extends BaseActivity<ActivityTotalPageBinding> implements BottomNavigationView.OnNavigationItemSelectedListener {
    //跳转到那个
    public int index = 0;
    //适配器
    private TabAdapter tabAdapter;
    //
    private List<Fragment> fragmentList = new ArrayList<>();
    //首页
    private HomeFragment homeFragment;
    //社交
    private SocialFragment socialFragment;
    //商城
    private MallFragment mallFragment;
    //我的
    private MineFragment mineFragment;
    //发布
    private ReleaseDialog releaseDialog;
    @Override
    public void initCreate() {
        super.initCreate();
        ImmersionBar.with(this).statusBarDarkFont(false).navigationBarColor(R.color.white).navigationBarDarkIcon(true).init();

    }

    @Override
    public int initContentView(Bundle savedInstanceState) {

        return R.layout.activity_total_page;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        index = intent.getExtras().getInt("index", 0);
        if (index == 0) {
            binding.ProhibitedVp.setCurrentItem(index, false);
        }
    }

    @Override
    public void initView() {
        super.initView();
        fragmentList.clear();
        homeFragment = new HomeFragment();
        fragmentList.add(homeFragment);
        socialFragment = new SocialFragment();
        fragmentList.add(socialFragment);
        mallFragment = new MallFragment();
        fragmentList.add(mallFragment);
        mineFragment = new MineFragment();
        fragmentList.add(mineFragment);
        boolean successful = PermissionUtils.isGranted(Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_BACKGROUND_LOCATION);
        if(!successful){
            XXPermissions.with(this).permission(Manifest.permission.ACCESS_COARSE_LOCATION,
                            Manifest.permission.ACCESS_FINE_LOCATION,
                            Manifest.permission.ACCESS_BACKGROUND_LOCATION).request(new OnPermissionCallback() {
                @Override
                public void onGranted(List<String> permissions, boolean all) {
                    if (all) {
                        ToastUtils.make().show("权限获取成功");
                        EasyPhotos.preLoad(mContext);
                    } else {
                        ToastUtils.make().show("权限获取失败");
                    }
                }
            });
        }
        //更新用户信息
        GeneralUtils.mydata(userMobile.getUser_id());

    }




    @Override
    public void initData() {
        super.initData();
        tabAdapter = new TabAdapter(fragmentList);
        binding.ProhibitedVp.setAdapter(tabAdapter);
        //true:滑动，false：禁止滑动
        binding.ProhibitedVp.setUserInputEnabled(false);

        releaseDialog = new ReleaseDialog(mContext);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        binding.navView.setOnNavigationItemSelectedListener(this);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull @NotNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.navigation_home:
                LogUtils.d("首页");
                binding.ProhibitedVp.setCurrentItem(0, false);
                break;
            case R.id.navigation_dashboard:
                LogUtils.d("社交");
                binding.ProhibitedVp.setCurrentItem(1, false);
                break;
            case R.id.navigation_publish:
                if(null != releaseDialog && !releaseDialog.isShowing()){
                    releaseDialog.show();
                }
                return false;
            case R.id.navigation_mall:
                LogUtils.d("商城");
                binding.ProhibitedVp.setCurrentItem(2, false);
                break;
            case R.id.navigation_mine:
                LogUtils.d("我的");
                binding.ProhibitedVp.setCurrentItem(3, false);
                break;
        }
        return true;
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if(null != mineFragment){
            //刷新用户信息
            mineFragment.refreshInformation();
        }
    }

    /**
     * 内部类适配器
     */
    private class TabAdapter extends FragmentStateAdapter {
        private List<Fragment> fragmentList;

        public TabAdapter(List<Fragment> fragmentList) {
            super(TotalPageActivity.this);
            this.fragmentList = fragmentList;
        }

        @Override
        public Fragment createFragment(int position) {
            return this.fragmentList == null ? null : fragmentList.get(position);
        }



        @Override
        public int getItemCount() {
            return this.fragmentList == null ? 0 : fragmentList.size();
        }
    }



}
